package com.concurrent.threads.bank;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ThreadTest
 * @Description 主程序，定义任务装载到线程里面
 * @Author li
 * Date 2021/8/21 22:11
 * Copyright
 **/
public class ThreadTest {
    public static final int DELAY = 10;
    public static final int INITIAL_ACCOUNT = 10;
    public static final int INITIAL_BALANCE = 100000;
    public static final double MAX_AMOUNT = 10000000;

    public static void main(String[] args) throws InterruptedException {
        Bank bank = new Bank(INITIAL_ACCOUNT, INITIAL_BALANCE);
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < MAX_AMOUNT; i++) {
            int fromAccount = i % INITIAL_ACCOUNT;
            Runnable task = () -> {
              try{
                  while (true){
                      int toAccount = (int) (bank.size()*Math.random());
                      double amount = Math.random() * MAX_AMOUNT * 0.1;
                      bank.transfer(fromAccount,toAccount,amount);
                      Thread.sleep((int)(DELAY * Math.random()));
                  }
              }catch (InterruptedException e){
                  e.printStackTrace();
              }
            };
            Thread thread = new Thread(task);
            thread.start();
            threads.add(thread);
        }
        for (Thread th :
                threads) {
            th.join();

        }

    }


}
